package gapp.ctrl;

import gapp.core.DomainClass;
import gapp.itg.Context;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


/**
 * A service control (and technically a listener) Doesn't change state just
 * calls service and return value to GUI.
 * 
 * NOTE: Act as converter from primitive data (int) to
 * domain objects (DomainObject).
 * 
 * @author hajo
 * 
 */
@SuppressWarnings("serial")
// Package private
class ServiceCtrl extends AbstractAction {

	// Set the text on button
	public ServiceCtrl(String string) {
		super(string);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// Get what we put:ed in in MainFrame
		JTextField in = (JTextField) this.getValue("MainFrame.jTextFieldIn");
		try {
			int i = Integer.parseInt(in.getText().trim());
			System.out.println("ServiceCtrl input = " + i);
			int j = Context.getSubSysService().doService(new DomainClass(i));
			in.setText(String.valueOf(j));
			// Revise this should not catch runtime exception
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null, "Bad number");
			// Nice we have the component here, control can change GUI
			in.grabFocus();
		}
	}

}
